﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Data.OracleClient;
using TimeTableEasy.src.Core;
using TimeTableEasy.src.Database;

namespace TimeTableEasy
{
	public partial class Window1
	{
        private Account _Account;
        private TimeTable _TimeTable;
        private CultureInfo _Locale;
        private Dictionary<String, String> _CampusListItems;
        private Dictionary<String, String> _SelectedCampusListItems;
        private Dictionary<String, String> _SelectedCursusListItems;

		public Window1(UInt32 id)
		{
            this.InitializeComponent();
            _Account = new Account(id);
            _TimeTable = new TimeTable(_Account);
            _Locale = new CultureInfo("fr-FR");
            
            MWUserLogged.Content = _Account.FirstName+" "+_Account.Name;
            _SelectedCampusListItems = new Dictionary<string, string>();
            _SelectedCursusListItems = new Dictionary<string, string>();
            Home_Calendar.EmptyCalendar();
            
            // Onglet Informations Personnelles
            Info_Name.Content = _Account.FirstName + " " + _Account.Name;
            Info_Age.Content = _Account.DateNaissance;
            Info_Email.Content = _Account.Mail;
            Info_Rue.Content = _Account.Adress;
            Info_City.Content = _Account.PostalCode + " " + _Account.City;
            Info_Tel.Content = _Account.Tel; 
         
            // Visibilité pour les admins
            if (_Account.IsAdmin)
            {
                Onglet_Admin.Visibility = Visibility.Visible;
                Onglet_Gestion.Visibility = Visibility.Visible;
                Load_ComboBox(Home_Combo1, _Account.Campus);
            }
            else if (_Account.IsManager)
                Load_ComboBox(Home_Combo1, _Account.Campus);


            TimeTableDate.Content = GetDateLine(_TimeTable.DateBegin, _TimeTable.DateEnd);

            for (int i = 0; i < _Account.Cursus.Count; i++)
            {
                Label temp = new Label();
                temp.FontFamily = new System.Windows.Media.FontFamily("Segoe WP SemiLight");
                temp.FontSize = 24;
                temp.HorizontalAlignment = HorizontalAlignment.Center;
                temp.Content = _Account.Cursus[i].ToString() + " " + _Account.Campus[i].ToString();
                Info_Cursus.Children.Add(temp);
            }

            if (_Account.IsStudent)
            {
                _TimeTable.GetPlaningClasse(null, null);
                for (UInt16 i = 0; i < _TimeTable.LenghtCurses.Count; i++)
                    Home_Calendar.AddEvent(_TimeTable.NameCurses[i], _TimeTable.SalleCurses[i], _TimeTable.DateCurses[i], _TimeTable.HourCurses[i], _TimeTable.LenghtCurses[i], _TimeTable.Position(_TimeTable.IsDouble[i]), _TimeTable.ColorCurses[i]);
            }
            else if (_Account.IsTeacher)
            {
                _TimeTable.GetPlaningFormateur(id);
                for (UInt16 i = 0; i < _TimeTable.LenghtCurses.Count; i++)
                    Home_Calendar.AddEvent(_TimeTable.NameCurses[i], _TimeTable.SalleCurses[i], _TimeTable.DateCurses[i], _TimeTable.HourCurses[i], _TimeTable.LenghtCurses[i], _TimeTable.Position(_TimeTable.IsDouble[i]), _TimeTable.ColorCurses[i]);
            }
            
            LoadEventTab();
		}

        private void Load_ComboBox(ComboBox list,ArrayList array)
        {
            if (!list.Items.IsEmpty)
                list.Items.Clear();

            for (UInt16 i = 0; i < array.Count; i++)
                list.Items.Add(array[i].ToString());

            if (list.Visibility != Visibility.Visible)
                list.Visibility = Visibility.Visible;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.GC.SuppressFinalize(_Account);
            new LoginWindow().Show();
            this.Close();
        }

        private void Mouse_Click_Next(Object sender, MouseButtonEventArgs e)
        {
            if (_Account.IsAdmin || _Account.IsManager)
                if ((Home_Combo1.SelectedValue == null) || (Home_Combo2.SelectedValue == null))
                    return;

            this._TimeTable.NextWeek();
            TimeTableDate.Content = GetDateLine(this._TimeTable.DateBegin, this._TimeTable.DateEnd);
            
            Home_Calendar.EmptyCalendar();

            if (_Account.IsStudent)
                _TimeTable.GetPlaningClasse(null, null);
            else if (_Account.IsTeacher)
                _TimeTable.GetPlaningFormateur(_Account.Id);
            else                
                _TimeTable.GetPlaningClasse(Home_Combo1.SelectedValue.ToString(), Home_Combo2.SelectedValue.ToString());

            for (UInt16 i = 0; i < _TimeTable.LenghtCurses.Count; i++)
                Home_Calendar.AddEvent( _TimeTable.NameCurses[i], _TimeTable.SalleCurses[i], _TimeTable.DateCurses[i], _TimeTable.HourCurses[i], _TimeTable.LenghtCurses[i], _TimeTable.Position(_TimeTable.IsDouble[i]), _TimeTable.ColorCurses[i]);
        }

        private void Mouse_Click_Previous(Object sender, MouseButtonEventArgs e)
        {
            if (_Account.IsAdmin || _Account.IsManager)
                if ((Home_Combo1.SelectedValue == null) || (Home_Combo2.SelectedValue == null))
                    return;

            this._TimeTable.PreviousWeek();
            TimeTableDate.Content = GetDateLine(this._TimeTable.DateBegin, this._TimeTable.DateEnd);

            Home_Calendar.EmptyCalendar();

            if (_Account.IsStudent)
                _TimeTable.GetPlaningClasse(null, null);
            else if (_Account.IsTeacher)
                _TimeTable.GetPlaningFormateur(_Account.Id);
            else
                _TimeTable.GetPlaningClasse(Home_Combo1.SelectedValue.ToString(), Home_Combo2.SelectedValue.ToString());

            for (int i = 0; i < _TimeTable.LenghtCurses.Count; i++)
                Home_Calendar.AddEvent(_TimeTable.NameCurses[i], _TimeTable.SalleCurses[i], _TimeTable.DateCurses[i], _TimeTable.HourCurses[i], _TimeTable.LenghtCurses[i], _TimeTable.Position(_TimeTable.IsDouble[i]), _TimeTable.ColorCurses[i]);
        }

        private String GetDateLine(DateTime Begin,DateTime End)
        {
            return "DU " + Begin.ToString("dd/MM/yyyy", this._Locale) +" AU " + End.ToString("dd/MM/yyyy", _Locale);
        }

        private void addUserBtn_Click(object sender, RoutedEventArgs e)
        {
            User_Edit au = new User_Edit("add");
            au.ShowDialog();
        }

        private void delUserBtn_Click(object sender, RoutedEventArgs e)
        {
            User_Choose du = new User_Choose("del");
            du.ShowDialog();
        }

        private void modUserBtn_Click(object sender, RoutedEventArgs e)
        {
            User_Choose mu = new User_Choose("mod");
            mu.ShowDialog();
        }

        private void addCampusBtn_Click(object sender, RoutedEventArgs e)
        {
            Campus_Edit am = new Campus_Edit(OpCodes.TimeTableEasyAction.ACTION_ADD,0);
            am.ShowDialog();
        }

        private void modCampusBtn_Click(object sender, RoutedEventArgs e)
        {
            new Campus_Choose(OpCodes.TimeTableEasyAction.ACTION_MOD).ShowDialog();     
        }

        private void delCampusBtn_Click(object sender, RoutedEventArgs e)
        {
            new Campus_Choose(OpCodes.TimeTableEasyAction.ACTION_DEL).ShowDialog();
        }

        private void addCursusBtn_Click(object sender, RoutedEventArgs e)
        {
            new Cursus_Edit(OpCodes.TimeTableEasyAction.ACTION_ADD).ShowDialog();
        }

        private void modCursusBtn_Click(object sender, RoutedEventArgs e)
        {
            new Cursus_Choose(OpCodes.TimeTableEasyAction.ACTION_MOD).ShowDialog();
        }

        private void delCursusBtn_Click(object sender, RoutedEventArgs e)
        {
            new Cursus_Choose(OpCodes.TimeTableEasyAction.ACTION_DEL).ShowDialog();
        }

        private void addClassBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void modClassBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void delClassBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void modEventBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void LoadEventTab()
        {
            GetCampusList(ref campusList, true, true);
        }

        private void GetCampusList(ref ComboBox cb, bool addOptionAll, bool root)
        {
            _CampusListItems = new Dictionary<string, string>();

            OracleDataReader array = OracleConnector.Query("SELECT CAM_NUM, CAM_NOM FROM CAMPUS");

            if (array == null) return;

            while (array.Read())
                _CampusListItems.Add(array.GetDecimal(0).ToString(), array.GetString(1));

            array.Dispose();

            Binding bind = new Binding();
            bind.Source = _CampusListItems;

            campusList.DisplayMemberPath = "Value";
            campusList.SelectedValuePath = "Key";
            campusList.SetBinding(ComboBox.ItemsSourceProperty, bind);
        }

        private void campusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cursusList.Items.Clear();
            if (campusList.SelectedIndex != 0)
            {
                cursusList.IsEnabled = true;
                addCursus.IsEnabled = true;

                cursusList.Items.Add("Tous les cursus");
                cursusList.SelectedIndex = 0;

                OracleDataReader array = OracleConnector.Query("SELECT CC_CursusNum FROM CAMPUS_CURSUS WHERE CC_CampusNum = " + campusList.SelectedValue.ToString());

                if (array == null) return;

                while(array.Read())
                    cursusList.Items.Add(array.GetString(0));

                array.Dispose();
            }
            else
            {
                addCursus.IsEnabled = false;
                cursusList.IsEnabled = false;
            }

        }

        private void addCampus_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (campusList.SelectedValue.ToString() == "All")
                {
                    _SelectedCampusListItems.Clear();
                    _SelectedCursusListItems.Clear();
                    _SelectedCursusListItems.Add("Tous les cursus", "All");
                }
                else
                {
                   _SelectedCampusListItems.Remove("All");
                   _SelectedCursusListItems.Remove("Tous les cursus");
                }
                _SelectedCampusListItems.Add(campusList.SelectedValue.ToString(), _CampusListItems[campusList.SelectedValue.ToString()].ToString());
                selectedCampus.SetBinding(ListBox.ItemsSourceProperty, "");
                Binding bind = new Binding();
                bind.Source = _SelectedCampusListItems;
                selectedCampus.DisplayMemberPath = "Value";
                selectedCampus.SelectedValuePath = "Key";
                selectedCampus.SetBinding(ListBox.ItemsSourceProperty, bind);


                selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, "");
                Binding b = new Binding();
                b.Source = _SelectedCursusListItems;
                selectedCursusList.DisplayMemberPath = "Key";
                selectedCursusList.SelectedValuePath = "Value";
                selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, b);
            }
            catch (ArgumentException)
            {
            }
        }

        private void addCursus_Click(object sender, RoutedEventArgs e)
        {
            if (_SelectedCampusListItems.ContainsKey("All"))
                _SelectedCampusListItems.Clear();

            if (_SelectedCursusListItems.ContainsValue("All"))
                _SelectedCursusListItems.Clear();

            try
            {
                _SelectedCursusListItems.Add(_CampusListItems[campusList.SelectedValue.ToString()].ToString() + ">" + cursusList.SelectedValue.ToString(), _CampusListItems[campusList.SelectedValue.ToString()].ToString());
                selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, "");
                Binding bind = new Binding();
                bind.Source = _SelectedCursusListItems;
                selectedCursusList.DisplayMemberPath = "Key";
                selectedCursusList.SelectedValuePath = "Value";
                selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, bind);
            }
            catch (Exception)
            {
            }

            try
            {
                _SelectedCampusListItems.Add(campusList.SelectedValue.ToString(), _CampusListItems[campusList.SelectedValue.ToString()].ToString());
                selectedCampus.SetBinding(ListBox.ItemsSourceProperty, "");
                Binding b = new Binding();
                b.Source = _SelectedCampusListItems;
                selectedCampus.DisplayMemberPath = "Value";
                selectedCampus.SelectedValuePath = "Key";
                selectedCampus.SetBinding(ListBox.ItemsSourceProperty, b);
            }
            catch (Exception)
            {
            }
        }

        private void removeSelectedCampus_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<String, String> temp = new Dictionary<string, string>();

            foreach (var line in _SelectedCursusListItems)
            {
                if (line.Value != _SelectedCampusListItems[selectedCampus.SelectedValue.ToString()].ToString())
                    temp.Add(line.Key, line.Value);
            }
            _SelectedCursusListItems = temp;
            selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, "");
            Binding b = new Binding();
            b.Source = _SelectedCursusListItems;
            selectedCursusList.DisplayMemberPath = "Key";
            selectedCursusList.SelectedValuePath = "Value";
            selectedCursusList.SetBinding(ListBox.ItemsSourceProperty, b);

            _SelectedCampusListItems.Remove(selectedCampus.SelectedValue.ToString());
            selectedCampus.SetBinding(ListBox.ItemsSourceProperty, "");
            Binding bind = new Binding();
            bind.Source = _SelectedCampusListItems;
            selectedCampus.DisplayMemberPath = "Value";
            selectedCampus.SelectedValuePath = "Key";
            selectedCampus.SetBinding(ListBox.ItemsSourceProperty, bind);
        }

        private void selectedCampus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (selectedCampus.SelectedIndex < 0)
                removeSelectedCampus.IsEnabled = false;
            else
                removeSelectedCampus.IsEnabled = true;
        }

        private void removeSelectedCursus_Click(object sender, RoutedEventArgs e)
        {
            // A faire
        }

        private void selectedCursusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (selectedCursusList.SelectedIndex < 0)
                removeSelectedCursus.IsEnabled = false;
            else
                removeSelectedCursus.IsEnabled = true;
        }

        private void Home_Combo1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Home_Calendar.EmptyCalendar();
            Load_ComboBox(Home_Combo2,Account.GetCursus(Home_Combo1.SelectedValue.ToString()));
        }

        private void Home_Combo2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Home_Calendar.EmptyCalendar();

            if (!(Home_Combo2.SelectedValue == null))
            {
                _TimeTable.GetPlaningClasse(Home_Combo1.SelectedValue.ToString(), Home_Combo2.SelectedValue.ToString());

                for (int i = 0; i < _TimeTable.LenghtCurses.Count; i++)
                    Home_Calendar.AddEvent(_TimeTable.NameCurses[i], _TimeTable.SalleCurses[i], _TimeTable.DateCurses[i], _TimeTable.HourCurses[i], _TimeTable.LenghtCurses[i], _TimeTable.Position(_TimeTable.IsDouble[i]), _TimeTable.ColorCurses[i]);
            }
        }
	}
}